﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Text;
using System.Threading;

public partial class _Default : System.Web.UI.Page
{
    #region 封装字段
    private string extname = string.Empty;
    private string newname = string.Empty;
    private string localpath = string.Empty;
    private DateTime date;

    /// <summary>
    /// 返回文件的扩展名
    /// </summary>
    public string Extname
    {
        get { return extname; }
        set { extname = value; }
    }
    /// <summary>
    /// 重新生成上传名
    /// </summary>
    public string Newname
    {
        get { return newname; }
        set { newname = value; }
    }
    /// <summary>
    /// 上传的物理路径
    /// </summary>
    public string Localpath
    {
        get { return localpath; }
        set { localpath = value; }
    }
    /// <summary>
    /// 返回当前时间
    /// </summary>
    public DateTime Date { get { return DateTime.Now; } }
    #endregion
    #region 事件
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    /// <summary>
    /// 上传处理
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnUpload_Click(object sender, EventArgs e)
    {
        ShowUpLoad();
    }
    #endregion
    #region 辅助方法
    /// <summary>
    /// 处理上传的方法
    /// </summary>
    private void ShowUpLoad()
    {
        StringBuilder strFileUploadInfo = new StringBuilder();
        HttpFileCollection uploadFiles = Request.Files;
        for (int i = 0; i < uploadFiles.Count; i++)
        {
            try
            {
                if (uploadFiles[i].FileName != "")
                {
                    extname = uploadFiles[i].FileName.Substring(uploadFiles[i].FileName.LastIndexOf('.')).ToLower();
                    newname = date.Year.ToString() + date.Month.ToString() + date.Day.ToString() + date.Hour.ToString() + date.Minute.ToString() + date.Second.ToString() + new Random().Next().ToString() + extname;
                    localpath = Server.MapPath(Request.ApplicationPath + ConfigurationManager.AppSettings["localPath"].ToString() + newname);
                    strFileUploadInfo.AppendLine(localpath + " 上传成功！<br />");
                    uploadFiles[i].SaveAs(localpath);
                }
            }
            catch (Exception ex)
            {
                this.Page.ClientScript.RegisterStartupScript(this.GetType(), Convert.ToString(Math.Round(1000000.00)), "<script>alert('" + ex.Message + "');</script>");
            }
        }
        strStatus.Text = "已经上传成功的详细信息如下：";
        strStatus.Text += "<br />";
        strStatus.Text += strFileUploadInfo.ToString();
    }
    #endregion
}
